package com.myapp.ruyixing.ui;

import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.DirectMessageDetail;
import com.myapp.ruyixing.model.DirectMessageDetail.ADirectMessageItem;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.KeyboardUtils;
import com.myapp.ruyixing.utils.LoginHelper;

public class DirectMessageDetailActivity extends BaseActivity {

	private TitleBar mTitleBar;
	private View mLoadingView;
	private ListView mListView;
	private TextView mEmptyView;

	private String mReadname;
	private DirectMessageDetail mDirectMessageDetail;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mDirectMessageDetail = (DirectMessageDetail) o;

			if (mDirectMessageDetail != null
					&& mDirectMessageDetail.items != null) {
				final MyListAdapter adapter = new MyListAdapter(
						mDirectMessageDetail.items);
				mListView.setAdapter(adapter);
				mListView.post(new Runnable() {
					@Override
					public void run() {
						// Select the last row so it will scroll into view...
						mListView.setSelection(adapter.getCount() - 1);
						EditText reply = (EditText) findViewById(R.id.reply_content);
						reply.setText("");
					}
				});
			}

			hideLoading();
		}

		@Override
		public void onFailure(Throwable throwable) {
			hideLoading();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_message_detail);

		initView();

		showLoading();

		mReadname = getIntent().getStringExtra("name");
		RYHttpClient.getMessageDetail(this, mHttpResponseHandler, mReadname);
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		findViewById(R.id.send).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText reply = (EditText) findViewById(R.id.reply_content);
				if (!TextUtils.isEmpty(reply.getText())) {
					RYHttpClient.sendDirectMessage(
							DirectMessageDetailActivity.this,
							mHttpResponseHandler, mReadname, reply.getText()
									.toString());
					showLoading();
					KeyboardUtils.setKeyboardVisibility(
							getApplicationContext(), reply, false);
				}

			}
		});
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private class ViewHolder {
		TextView mTitleView;
		TextView mTimeView;
		TextView mContentView;
		NetworkImageView mAvatarView;
	}

	private class MyListAdapter extends ArrayAdapter<ADirectMessageItem> {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter(List<ADirectMessageItem> list) {
			super(DirectMessageDetailActivity.this, -1, list);
			mQueue = Volley.newRequestQueue(DirectMessageDetailActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public int getItemViewType(int position) {
			return LoginHelper.getInstance().getUserId()
					.equals(getItem(position).username) ? 0 : 1;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				if (getItemViewType(position) == 0) {
					convertView = LayoutInflater.from(getContext()).inflate(
							R.layout.cell_message_detail_me, parent, false);
				} else {
					convertView = LayoutInflater.from(getContext()).inflate(
							R.layout.cell_message_detail_other, parent, false);
				}

				ViewHolder holder = new ViewHolder();
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);
				holder.mContentView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mAvatarView = (NetworkImageView) convertView
						.findViewById(R.id.avatar);
				convertView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			ADirectMessageItem item = getItem(position);

			holder.mContentView.setText(item.content);
			holder.mTimeView.setText(item.datetime);

			String avatar = getItemViewType(position) == 0 ? mDirectMessageDetail.avatar
					: mDirectMessageDetail.avatar_read;

			if (!TextUtils.isEmpty(avatar)) {
				holder.mAvatarView.setImageUrl(avatar, mImageLoader);
			}

			return convertView;

		}
	}
}
